<template>
  <div>
    三级分类
    <el-tree
      :data="menus"
      :props="defaultProps"
      :expand-on-click-node="false"
      :default-expanded-keys="expendedKeys"
      node-key="catId"
      draggable
      :allow-drop="allowDrop"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)">
            Append
          </el-button>
          <el-button
            v-if="node.childNodes.length === 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)">
            Delete
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => edit(data)">
            Edit
          </el-button>
        </span>
      </span>
    </el-tree>

    <!--   添加dialog   -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="category">
        <el-form-item label="活动名称">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="category.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input v-model="category.productUnit" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'category',
  methods: {
    getMenuData () {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then(({data}) => {
        // console.log(data)
        this.menus = data.data
      })
    },
    submitData () {
      if (this.dialogType === 'add') {
        this.updateCategory()
      } else if (this.dialogType === 'edit') {
        this.editCategory()
      }
    },
    append (data) {
      console.log(data, '添加分类')
      this.dialogFormVisible = true
      this.dialogType = 'add'
      this.dialogTitle = '添加分类'
      this.category.parentCid = data.catId
      this.category.catLevel = data.catLevel + 1
    },
    edit (target) {
      console.log(target)
      this.dialogFormVisible = true
      this.dialogTitle = '编辑分类'
      this.dialogType = 'edit'
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${target.catId}`),
        method: 'get',
      }).then(({data}) => {
        this.category.catId = data.data.catId
        this.category.catLevel = data.data.catLevel
        this.category.name = data.data.name
        this.category.icon = data.data.icon
        this.category.productUnit = data.data.productUnit
        this.category.sort = data.data.sort
        this.category.showStatus = data.data.showStatus
        this.category.parentCid = data.data.parentCid

      })
    },
    remove (node, data) {
      this.$confirm(`确定进行[${data.name}]删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = [data.catId]
        this.$http({
          url: this.$http.adornUrl('/product/category/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500
          })
          this.getMenuData()
          this.expendedKeys = [node.parent.data.catId]
          console.log(node)
        })
      }).catch(() => {
      })
      console.log('remove', node, data)
    },
    updateCategory () {
      this.dialogFormVisible = false
      this.$http({
        url: this.$http.adornUrl('/product/category/save'),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({data}) => {
        this.$message({
          message: '添加成功',
          type: 'success',
          duration: 1500
        })
        this.getMenuData()
        this.expendedKeys = [this.category.parentCid]
      })
    },
    editCategory () {
      this.dialogFormVisible = false
      let {catId, name, icon, productUnit} = this.category
      this.$http({
        url: this.$http.adornUrl('/product/category/update'),
        method: 'post',
        data: this.$http.adornData({catId, name, icon, productUnit}, false)
      }).then(({data}) => {
        this.$message({
          message: '更新成功',
          type: 'success',
          duration: 1500
        })
        this.getMenuData()
        this.expendedKeys = [this.category.parentCid]
      })
    },
    allowDrop (draggingNode, dropNode, type) {
      let deep = this.countNodeLevel(draggingNode.data, 0)
      let target = dropNode.data.catLevel
      return deep + target <= 3
    },
    countNodeLevel (target, level) {
      console.log('level', level)
      let max = level + 1
      for (let index = 0; index < target.children.length; ++index) {
        let child = target.children[index]
        let res = this.countNodeLevel(child, level + 1)
        max = res > max ? res : max
      }
      return max
    }
  },
  created () {
    this.getMenuData()
  },
  data () {
    return {
      dialogType: '',
      dialogTitle: '',
      category: {
        name: '',
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        catId: null,
        icon: '',
        productUnit: ''
      },
      menus: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      expendedKeys: [],
      dialogFormVisible: false
    }
  }
}
</script>

<style scoped>

</style>
